package edu.tgc.web.controller.code;

import java.util.ArrayList;
import java.util.UUID;

import org.abyss.common.web.controller.easyui.EasyuiResultBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import edu.tgc.entity.code.TbScale;
import edu.tgc.service.code.CodeService;

@Controller
@RequestMapping(value = "/code/scale")
public class ScaleController {
    @Autowired
    private CodeService service;

    @RequestMapping(value = "/create")
    public String create(String id, Integer xmTypeId, ModelMap map) {
        TbScale model = new TbScale();
        if (StringUtils.hasText(id)) {
            model = this.service.findScaleById(UUID.fromString(id));
        }
        map.put("model", model);
        return "code/scale/create";
    }

    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(String id) {
        try {
            this.service.deleteScale(UUID.fromString(id));
            return EasyuiResultBuilder.buildAjaxResult("删除成功", true, "scale_list");
        } catch (Exception e) {
            return EasyuiResultBuilder.buildAjaxResult("删除失败");
        }
    }

    @RequestMapping(value = "/manage")
    public String manage() {
        return "code/scale/manage";
    }

    @RequestMapping(value = "/page")
    @ResponseBody
    public Object page(Integer xmTypeId, Pageable pageable) {

        if (xmTypeId != null) {
            Page<TbScale> page = this.service.getScalePageByProjectId(xmTypeId, pageable);
            return EasyuiResultBuilder.buildDataGridResult(page);
        }
        return EasyuiResultBuilder.buildDataGridResult(0, new ArrayList<>());
    }

    @RequestMapping(value = "/save")
    @ResponseBody
    public Object save(TbScale scale) {
        try {
            this.service.scaleSave(scale);
            return EasyuiResultBuilder.buildAjaxResult("保存成功", true, "scale_list");
        } catch (Exception e) {
            return EasyuiResultBuilder.buildAjaxResult("保存失败");
        }
    }

    @RequestMapping(value = "/update")
    public String update(String id, ModelMap map) {
        TbScale model = new TbScale();
        if (StringUtils.hasText(id)) {
            model = this.service.findScaleById(UUID.fromString(id));
        }
        map.put("model", model);
        return "code/scale/create";

    }
}
